package cn.xianyang.org.controller;

import cn.xianyang.basic.util.AjaxResult;
import cn.xianyang.basic.util.PageList;
import cn.xianyang.org.domain.DictionaryDetail;
import cn.xianyang.org.query.DictionaryDetailQuery;
import cn.xianyang.org.service.IDictionaryDetailService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * resultFul风格接口
 */
@RestController //组合注解 controller + responseBody 返回json格式。如果返回页面就单独用controller
@RequestMapping("/cdxj")
@Api(tags = "员工接口",description = "员工接口详细描述")
public class DictionaryDetailController {
    @Autowired
    private IDictionaryDetailService dictionaryDetailService;

    /**
     * 查询一条记录  动词查询getMapping
     * @param
     * @return
     */
    @GetMapping("/{id}")
    public DictionaryDetail queryById(@PathVariable("id") Long id){
        return dictionaryDetailService.queryById(id);
    }

    /**
     * 查询全部记录  动词查询getMapping
     */
    @GetMapping
    public List<DictionaryDetail> queryAll(){
        return dictionaryDetailService.queryAll();
    }

    /**
     * 新增&修改记录  动词查询putMapping
     */
    @PutMapping
    public AjaxResult AddOrUpdate(@RequestBody DictionaryDetail dictionaryDetail) {
        try {
            if (dictionaryDetail.getId() == null) {
                dictionaryDetailService.save(dictionaryDetail);
            } else {
                dictionaryDetailService.update(dictionaryDetail);
            }
            return AjaxResult.me();
        } catch (Exception exception) {
            exception.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("抱歉，我们正在殴打程序员，稍后再试");
        }
    }

    /**
     * 删除记录     动词查询deleteMapping
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            dictionaryDetailService.removeById(id);
            return AjaxResult.me();
        } catch (Exception exception) {
            exception.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("抱歉，我们正在殴打程序员，稍后再试");
        }
    }


    /**
     * 高级查询+分页     动词查询postMapping
     * @RequestBody 从请求体中获取参数
     */
    @PostMapping
    public PageList<DictionaryDetail> queryPage(@RequestBody DictionaryDetailQuery query){
        return dictionaryDetailService.queryPage(query);
    }

    /**
     * 批量删除     动词查询PatchMapping
     */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            dictionaryDetailService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception exception) {
            exception.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("抱歉，我们正在殴打程序员，稍后再试");
        }
    }

}
